package com.example.testdemo.demos.web.Controller;

import com.example.testdemo.demos.web.Service.QRCodeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import com.alibaba.fastjson.JSONObject;
import com.example.testdemo.demos.web.Config.Result;
import com.example.testdemo.demos.web.Service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;


@RestController
@RequestMapping("/file")
public class UploadController {
    @Autowired
    private UploadService uploadService;
    @Autowired
    private QRCodeService qrCodeService;
    @Value("${file.downloadPath}")
    private String downloadPath;
    @Value("${file.apkPath}")
    private String apkPath;

    /**
     * 文件上传接口
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result Upload(@RequestParam("file") MultipartFile file) {
        return uploadService.fileUpload(file);
    }

    /**
     * 删除文件
     *
     * @param file
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody JSONObject file) {
        return uploadService.fileDelete(String.valueOf(file.get("file")));
    }


    /**
     * 文件下载接口
     */
    @GetMapping("/download")
    public ResponseEntity<FileSystemResource> downloadFile(@RequestParam("file") String fileName) throws IOException {
        // 加载文件作为资源
        File file = new File(downloadPath+fileName);
        FileSystemResource resource = new FileSystemResource(file);

        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename="+fileName);

        // 返回文件内容
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(resource);
    }

    /**
     * 下载安装包
     * @return
     * @throws IOException
     */
    @GetMapping("/downloadApk")
    public ResponseEntity<FileSystemResource> downloadFileApk() throws IOException {
        // 加载文件作为资源
        File file = new File(apkPath);
        FileSystemResource resource = new FileSystemResource(file);

        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=UNI__9D0B014.apk");

        // 返回文件内容
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(resource);
    }


    @GetMapping("/QR")
    public Result QRcode(@RequestParam("content") String content) throws Exception {
        return qrCodeService.generateQR(content);
    }
}